package com.example.citypolitics.Tools;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Record;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class RecordTool {
    public static List<Record> jsonToRecordList(String json, String key) {
        List<Record> rList = new ArrayList<Record>();
        if (json != null && json != "null" && !json.isEmpty()) {
            JSONObject jb = JSON.parseObject(json);
            JSONArray array = jb.getJSONArray(key);
            if (array != null)
                for (Object s : array) {
                    //    for (int i = 0; i < array.size(); i++) {
                    //JSONArray中的数据转换为String类型需要在外边加"";不然会报出类型强转异常！
                    String str = s + "";
                    //  JSONObject object = JSON.parseObject(str);
                    Map map = JSONObject.parseObject(str, Map.class);
                    Record r = new Record();
                    r.setColumns(map);
                    r.set("str", str);
                    rList.add(r);
                    //     System.err.println("RecordTool==================="+r.toString());
                }
        }

        return rList;
    }

}
